import gtk
import friends

#TODO : build a tool to select friends
def selectFriends():
    """launch the friendsselector gui and return a friendsgroup selected by user"""
    app = guiFriendsSelector()
    app.dialog.run()
    return app.getFr()

class guiFriendsSelector:
    def __init__(self):
        #init UI
        builder = gtk.Builder()
        builder.add_from_file("ui/friendsSelector.glade")
        self.dialog = builder.get_object("friendsSelectorDialog")
        builder.connect_signals(self)
        #buind widget
        self._friendsListstore = builder.get_object("FriendsListstore")
        self.refreshFriendsList()
        
    def refreshFriendsList(self):
        #populate liststore
        self._friendsListstore.clear()
        for f in friends.getFriendsPool().getAllFriends():
            #append to liststore
            self._friendsListstore.append((f.getName(),f.getID(),False))
        
    def getFr(self):
        iter = self._friendsListstore.get_iter_first()
        fr = friends.friendsGroup([])
        while iter:
            if self._friendsListstore.get_value(iter,2):
                fr.append(friends.getGlobalAccount(self._friendsListstore.get_value(iter,1)))
            iter = self._friendsListstore.iter_next(iter)
        return fr
    
    def on_cancelAction_activate(self,widget,data=None):
        self.dialog.destroy()
        
    def on_OKAction_activate(self,widget,data=None):
        self.dialog.destroy()
        
    def on_cell_friend_toggled(self,cellrenderertoggle,path):
        iter = self._friendsListstore.get_iter(path)
        self._friendsListstore.set_value(iter,2,not cellrenderertoggle.get_active())

    